﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.City6473.WebSite.Controllers;
using System.Web.Mvc;
using System.Web.Security;



namespace SpaceNeedle.City6473.WebSite.Security.Mvc
{
    public class Secure : ActionFilterAttribute 
    {
        private void RedirectToLoginPage(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Redirect("~/Users/Login", true);
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
  
     {
            var request = filterContext.HttpContext.Request;

            if ( request.Cookies == null || request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                filterContext.Result = new SecureController().RedirectToLoginPage();
            }

            if (Auth.CurrentUser == null)
            {
                filterContext.Result = new SecureController().RedirectToLoginPage();
            }
            
            base.OnActionExecuting(filterContext);
        }
    }  
}